package com.pulanit.pangu.admin.web.controller.business;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pulanit.pangu.admin.business.api.entity.Article;
import com.pulanit.pangu.admin.business.api.entity.ArticleExpert;
import com.pulanit.pangu.admin.business.api.service.IArticleExpertService;
import com.pulanit.pangu.admin.business.api.param.ArticleExpertPageIn;
import cn.hutool.core.lang.Assert;
import com.gitee.pulanos.pangu.framework.sdk.dto.Result;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PageResult;
import com.pulanit.pangu.admin.business.api.service.IArticleService;
import com.pulanit.pangu.admin.common.AppContext;
import com.pulanit.pangu.admin.system.api.entity.UserEntity;
import com.pulanit.pangu.admin.system.dao.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PagingUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文稿专家审核表 前端控制器
 * </p>
 *
 * @author 天鸣
 * @since 2024-03-06
 */
@RestController
@RequestMapping("/api/business/articleExpert")
public class ArticleExpertController {

    @Autowired
    private IArticleExpertService articleExpertService;

    @Autowired
    private IArticleService articleService;

    @Autowired
    private UserMapper userMapper;


    /**
     * 接审专家列表-分页
     */
    @GetMapping("/page")
    public Result<PageResult<ArticleExpert>> list(ArticleExpertPageIn articleExpertPageIn)
    {
        Page page = PagingUtil.createPage(articleExpertPageIn);
        LambdaQueryWrapper<ArticleExpert> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleExpert::getExpertUserId, AppContext.getUserInfo().getId())
                .eq(ObjectUtil.isNotEmpty(articleExpertPageIn.getKeyword()),ArticleExpert::getRefuse,articleExpertPageIn.getKeyword());
        Page<ArticleExpert> resultPage = articleExpertService.page(page, wrapper);
        resultPage.getRecords().forEach(item->{
            Article article = articleService.getById(item.getArticleId());
            item.setArticleTitle(article.getArticleTitle());
            UserEntity userEntity = userMapper.selectById(item.getEditUserId());
            item.setEditUserName(userEntity.getName());
        });
        return Result.success(PagingUtil.transformPageResult(resultPage));
    }

    /**
     * 接审专家管理列表-分页
     */
    @GetMapping("/managerPage")
    public Result<PageResult<ArticleExpert>> managerList(ArticleExpertPageIn articleExpertPageIn)
    {
        Page page = PagingUtil.createPage(articleExpertPageIn);
        LambdaQueryWrapper<ArticleExpert> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleExpert::getEditUserId, AppContext.getUserInfo().getId())
                .eq(ObjectUtil.isNotEmpty(articleExpertPageIn.getRefuse()),ArticleExpert::getRefuse,articleExpertPageIn.getRefuse())
                .eq(ObjectUtil.isNotEmpty(articleExpertPageIn.getCheckStatus()),ArticleExpert::getCheckStatus,articleExpertPageIn.getCheckStatus());
        wrapper.orderByDesc(ArticleExpert::getArticleId);
        Page<ArticleExpert> resultPage = articleExpertService.page(page, wrapper);
        resultPage.getRecords().forEach(item->{
            Article article = articleService.getById(item.getArticleId());
            item.setArticleTitle(article.getArticleTitle());
            item.setFieldId(article.getFieldId());
            UserEntity userEntity = userMapper.selectById(item.getExpertUserId());
            item.setExpertUserName(userEntity.getName());
        });
        return Result.success(PagingUtil.transformPageResult(resultPage));
    }

    /**
    * 获取列表
    *
    * @return
    */
    @GetMapping("/list")
    public Result<List<ArticleExpert>> list() {
       List<ArticleExpert> list = articleExpertService.list();
       return Result.success(list);
    }

    /**
     * 是否审核
     *
     * @return
     */
    @GetMapping("/isCheck")
    public Result<Integer> isCheck(@RequestParam("id") Long id) {
        LambdaQueryWrapper<ArticleExpert> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleExpert::getArticleId, id)
                .eq(ArticleExpert::getExpertUserId, AppContext.getUserInfo().getId())
                .last("limit 1");
        ArticleExpert articleExpert = articleExpertService.getOne(wrapper);
        return Result.success(articleExpert.getCheckStatus());
    }

    /**
     * 获取详情
     * @return
     */
    @GetMapping("/detail")
    public Result<ArticleExpert> detail(@RequestParam("id") Long id){
        Assert.notNull(id, "ID 不能为空");
        ArticleExpert articleExpert = articleExpertService.getById(id);
        return Result.success(articleExpert);
    }

    /**
     * 保存
     * @param articleExpert
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody ArticleExpert articleExpert){
        articleExpertService.save(articleExpert);
        return Result.success();
    }

    /**
     * 接收
     * @param id
     * @return
     */
    @PostMapping("/accept")
    public Result<Void> accept(@RequestParam("id") Long id){
        Assert.notNull(id, "ID 不能为空");
        ArticleExpert articleExpert = new ArticleExpert();
        articleExpert.setId(id);
        articleExpert.setRefuse(1);
        articleExpertService.updateById(articleExpert);
        return Result.success();
    }

    /**
     * 拒绝
     * @param id
     * @return
     */
    @PostMapping("/refuse")
    public Result<Void> refuse(@RequestParam("id") Long id){
        Assert.notNull(id, "ID 不能为空");
        ArticleExpert articleExpert = new ArticleExpert();
        articleExpert.setId(id);
        articleExpert.setRefuse(2);
        articleExpertService.updateById(articleExpert);
        return Result.success();
    }

    /**
     * 修改
     * @param articleExpert
     * @return
     */
    @PostMapping("/update")
    public Result<Void> update(@RequestBody ArticleExpert articleExpert){
        articleExpertService.updateById(articleExpert);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public Result<Void> delete(@RequestParam("id") Long id){
        Assert.notNull(id, "ID 不能为空");
        articleExpertService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     *
     */
     @PostMapping("/batchDelete")
     public Result<Void> batchDelete(@RequestParam List<Long> ids) {
        Assert.notEmpty(ids, "ID 不能为空");
        articleExpertService.removeBatchByIds(ids);
        return Result.success();
     }

}
